#coding=utf-8
import random
import sys
from django.shortcuts import HttpResponse
import json
from IR.Util.Variables import Constant
from IR.Util.DB import DB
from  IR.moduels.snippet import Snippet
from  IR.moduels.Indexing import Search
import jieba
import re
import math
import heapq
from pymongo import MongoClient
import Util

reload(sys)
sys.setdefaultencoding('utf8')

class_mapping=dict()
class_mapping["国内"]="guonei"
class_mapping["国际"]="guoji"
class_mapping["社会"]="shehui"
class_mapping["体育"]="sports"
class_mapping["娱乐"]="ent"
class_mapping["军事"]="war"
class_mapping["科技"]="tech"
class_mapping["财经"]="money"
index_path=u"C:/InformationRetrieval/IR_Final/IR/moduels/Indexing/IndexData/"

def search(request):
    if (request.method == "POST"):
        query_text = request.POST.get(Constant.QUERY_TEXT, "")
        #pagenum从0开始
        page_num = int(request.POST.get(Constant.PAGE_NUM, 0))
        count = int(request.POST.get(Constant.COUNT, 20))
        sort_mode = int(request.POST.get(Constant.SORT_MODE, 0))
        result = dict()
        print(query_text)
        if (query_text == "" or len(query_text) <= 0):
            result[Constant.ERROR_CODE] = 1
            result[Constant.ERROR_MESSAGE] = "key_word is null"
            result[Constant.STATUS] = Constant.FAIL
            return HttpResponse(json.dumps(result), content_type="application/json")
        res_list=None
        # try:
        news_list=list()
        if(Util.get_index_from_redis("has_data")==None):
            if(Util.save_index_to_redis()):
                print ("将索引存入内存成功！")
                InvertedIndex = json.loads(Util.get_index_from_redis("InvertedIndex"))
                word_df = json.loads(Util.get_index_from_redis("word_df"))
                documentLengths = json.loads(Util.get_index_from_redis("documentLengths"))
                gbk_stop_words = Util.get_index_from_redis("stop_words").split('\n')
                stop_words = [x.strip().decode('gbk') for x in gbk_stop_words]
            else:
                print ("将索引存入内存失败！正在启用独立处理程序。。。")
                #将索引等信息读到内存中
                with open(u"./IndexData/InvertedIndex.json", 'r') as f:
                    InvertedIndex = json.load(f)
                with open(u"./IndexData/word_df.json", 'r') as f:
                    word_df = json.load(f)
                with open(u"./IndexData/permutermIndex.json", 'r') as f:
                    permIndex = json.load(f)
                with open(u"./IndexData/documentVectors.json", 'r') as f:
                    documentVectors = json.load(f)
                with open(u"./IndexData/documentLengths.json", 'r') as f:
                    documentLengths = json.load(f)
                with open(u"./IndexData/stop_words.txt", 'r') as f:
                    gbk_stop_words = f.readlines()
                    stop_words = [x.strip().decode('gbk') for x in gbk_stop_words]
        else:
            InvertedIndex = json.loads(Util.get_index_from_redis("InvertedIndex"))
            word_df = json.loads(Util.get_index_from_redis("word_df"))
            documentLengths = json.loads(Util.get_index_from_redis("documentLengths"))
            gbk_stop_words = Util.get_index_from_redis("stop_words").split('\n')
            stop_words = [x.strip().decode('gbk') for x in gbk_stop_words]
            print (u"成功从内存读取索引！")

        K = 30
        # 2.连接MongoDB数据库
        client = MongoClient('localhost', 27017)
        news_db = client.news
        wangyi_news = news_db.wangyi_news
        query_wordset = Search.processQuery(query_text,stop_words)
        topK_result = Search.fastCosScore(K,query_wordset,documentLengths,InvertedIndex,word_df)
        if len(topK_result)==0:
            print 'Sorry,no revelant news...'
        else:
            findConditions = []
            for w in topK_result:
                temp = {}
                temp['news_id'] = w[0]
                findConditions.append(temp)
            topK_news = list(wangyi_news.find({'$or':findConditions}))
            begin=count*page_num
            if(begin>len(topK_news)):
                result[Constant.STATUS] = Constant.FAIL
                result[Constant.ERROR_CODE] = 3
                result[Constant.ERROR_MESSAGE] = "there is not enough news"
                return HttpResponse(json.dumps(result), content_type="application/json")
            end=count*(page_num+1)
            if(end>len(topK_news)):
                end=len(topK_news)
            news_list=None
            if(len(topK_news)>0 and begin>=0 and end>=0):
                for i in range(begin,end):
                    news = dict()
                    news["relativity"] = ""  # 相关度
                    time = str(topK_news[i]["news_pub_time"]).replace('/', '-')
                    time = time[6:10] + "-" + time[:5] + " " + time[10:]
                    news["time"] = time
                    news["content"] = topK_news[i]["news_content"]  # 内容
                    news["title"] = topK_news[i]["news_title"]  # 标题
                    news["keywords"] = topK_news[i]["news_keyword"]  # 关键词
                    news["comment_num"] = topK_news[i]["news_comment_num"]  # 评论数
                    news["url"] = topK_news[i]["news_url"]  # 网页链接
                    news["source"]=topK_news[i]["news_source_website"]

                    snippet = Snippet()
                    # IDs=[u'D54T85S30001875N']
                    IDs=list()
                    IDs.append(str(topK_news[i]["news_id"]))
                    # 生成预览
                    preview = snippet.create_preview(IDs)
                    news["preview"] = preview  #
                    news["commendatory_num"] = "暂无"  # 褒义评论数

                    news["snippet"] =  snippet.create_snippet(query_text, IDs)
                    news["relativity_news"] = "暂无"  # 相关新
                    news_list.append(news)
            else:
                result[Constant.STATUS] = Constant.FAIL
                result[Constant.ERROR_CODE] = 2
                result[Constant.ERROR_MESSAGE] = "news length error. length:"+str(len(topK_news))+"begin:"+str(begin)+"end:"+str(end)
                return HttpResponse(json.dumps(result), content_type="application/json")
        news_list = Util.sort_news(news_list, sort_mode)
        result[Constant.STATUS] = Constant.SUCCESS
        result[Constant.NWES_LIST] =news_list
        return HttpResponse(json.dumps(result), content_type="application/json")

        # except Exception, e:
        #     print (e.message)
        #     result[Constant.STATUS] = Constant.FAIL
        #     result[Constant.ERROR_CODE] = 2
        #     result[Constant.ERROR_MESSAGE] = e.message
        #     return HttpResponse(json.dumps(result), content_type="application/json")
